package com;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ami.weather.R;
import com.jy.unkown.AD.UnkownSplashAd;
import com.jy.unkown.AD.UnkwonReward;
import com.jy.unkown.AdReqCallBack;
import com.jy.unkown.inter.IRewardCallBack;
import com.jy.unkown.inter.ISplashCallBack;
import com.jy.utils.call.NoDoubleClick;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

public class SplashActivity extends BBActivity {
    static final String TAG = "Splash";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_splash);

        findViewById(R.id.jiazai).setOnClickListener(new NoDoubleClick() {
            @Override
            public void noDoubleCall(View view) {
                loadAd();
            }
        });
        findViewById(R.id.zhanshi).setOnClickListener(new NoDoubleClick() {
            @Override
            public void noDoubleCall(View view) {
                zhanshi();
            }
        });
    }


    UnkownSplashAd unkownSplashAd;

    ISplashCallBack adCallBack = new ISplashCallBack() {
        @Override
        public void skip() {
            Log.e(TAG, "skip ");
        }

        @Override
        public void onADTick(long l) {
            Log.e(TAG, "onADTick " + l);
        }

        @Override
        public void close() {
            finish();
            Log.e(TAG, "关闭");
        }

        @Override
        public void click() {
            Log.e(TAG, "点击");
        }

        @Override
        public void exposure() {
            Log.e(TAG, "曝光成功");
        }

        @Override
        public void err() {
            Log.e(TAG, "曝光失败");
        }
    };


    AtomicBoolean isRequesting = new AtomicBoolean(false);
    AtomicBoolean isRequestSucc = new AtomicBoolean(false);

    AdReqCallBack adReqCallBack = new AdReqCallBack() {
        @Override
        public void reqSucc() {
            Log.e(TAG, "拉取广告成功");
            showToast("拉取广告成功");
            isRequestSucc.set(true);
            isRequesting.set(false);
        }

        @Override
        public void reqErr() {
            Log.e(TAG, "拉取广告失败");
            showToast("拉取广告失败");
            isRequesting.set(false);
        }
    };


    protected void loadAd() {
        if (isRequesting.get()) {
            return;
        }
        if (unkownSplashAd == null) {
            unkownSplashAd = new UnkownSplashAd();
        }
        unkownSplashAd.setAdCallBack(adCallBack);
        unkownSplashAd.setRequestId(UUID.randomUUID().toString());
        unkownSplashAd.setAdReqCallBack(adReqCallBack);
        unkownSplashAd.setAppId("QMCAYW");

        unkownSplashAd.setAppId(Key.APPID);
        unkownSplashAd.setAdId(Key.SPLASH);
        unkownSplashAd.setPrice(0);


        isRequesting.set(true);
        isRequestSucc.set(false);
        unkownSplashAd.loadAd();
    }


    protected void zhanshi() {
        if (!isRequestSucc.get()) {
            showToast("广告未请求成功");
            return;
        }
        ViewGroup viewGroup = findViewById(R.id.adRL);
        unkownSplashAd.showAD(viewGroup);
    }
}
